package com.zzyl.common.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.IOException;
import java.io.InputStream;

public class PDFUtil {
    public static String pdfToString(InputStream inputStream){
        PDDocument document = null;
        try {
            //加载PDF文档
            document = PDDocument.load(inputStream);
            // 创建一个PDFTextStripper实例来提取文本
            PDFTextStripper pdfTextStripper = new PDFTextStripper();
            // 从PDF文档中提取文本
            String text = pdfTextStripper.getText(document);
            return text;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                inputStream.close();
                document.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
